/*



 */
package com.bbh.plugin.single.privilege.demo.controller;


import com.bbh.plugin.single.privilege.core.entity.Area;
import com.bbh.plugin.single.privilege.core.enums.BaseEnum;
import com.bbh.plugin.single.privilege.core.service.AreaService;
import com.bbh.plugin.single.privilege.core.vo.tree.AreaVo;
import com.bbh.plugin.single.privilege.core.vo.tree.TreeTableVo;
import com.bbh.plugin.single.privilege.core.vo.tree.ZTreeNoneAjaxVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.domain.Message;
import com.bbh.common.domain.StatusType;
import com.bbh.common.utils.IdUtils64;
import com.bbh.common.utils.mapper.JsonMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;

/**
 * Controller - 地区
 *
 * @author KCSHOP Team
 * @version 3.0
 */
@Controller
@RequestMapping("/area")
public class AreaController extends BasicController {
    @Autowired
    private AreaService areaService;
    private String uuid;

    /**
     * 跳转菜单页面
     *
     * @author bestgwd
     */
    @RequestMapping(value = VIEW_FILED + "/index")
    public String index(ModelMap model, HttpServletRequest request) {
        model.addAttribute("statusItems", EnumSet.allOf(BaseEnum.IS_ENABLED.class));
        return "single/area/index";
    }

    @RequestMapping(value = INSERT_FILED + "/insert")
    public
    @ResponseBody
    Message insert(@Valid Area area) {
        Date date = new Date();
        area.setCreateTime(date);
        area.setCreateBy(getPrincipal().getLoginName());
        area.setModifyTime(date);
        area.setModifyBy(getPrincipal().getLoginName());
        area.setId(IdUtils64.getInstance().getStringId());
        if ("-1".equals(area.getParentId())) {
            area.setType("0");
        } else {
            Area area1 = areaService.selectByPrimaryKey(area.getParentId());
            Integer t = (Integer.parseInt(area1.getType()) + 1);
            area.setType(t.toString());
        }

        areaService.insert(area);
        return new Message(StatusType.SUCCESS.getKey(), "保存成功");
    }

    @RequestMapping(value = VIEW_FILED + "/list")
    public
    @ResponseBody
    TreeTableVo list1(String id, Model model, String name, String isEnabled) {
        Condition params = new Condition();
        params.put("parentId", null == id ? -1 : id);
        if (StringUtils.isNotBlank(name)) {
            params.put("name", name);
        }
        if (StringUtils.isNotBlank(isEnabled)) {
            params.put("isEnabled", isEnabled);
        }
        List<AreaVo> areaVoList = areaService.findList(params, null);
        TreeTableVo treeTableVo = new TreeTableVo();
        treeTableVo.setRows(areaVoList);
        treeTableVo.setTotal(1l);
        return treeTableVo;
    }

    /**
     * 跳转到edit页面
     *
     * @return
     */
    @RequestMapping(value = UPDATE_FILED + "/edit")
    public String edit(String id, Model model, String type) {
        Area area;
        if ("edit".equals(type)) {
            area = areaService.selectByPrimaryKey(id);
            model.addAttribute("area", area);
            if (!"-1".equals(area.getParentId())) {
                Area ar = areaService.selectByPrimaryKey(area.getParentId());
                model.addAttribute("ar", ar);
            }
            model.addAttribute(OPERATE_TYPE, "edit");
        }
        if ("add".equals(type)) {
            if (StringUtils.isNotBlank(id) && !"0".equals(id)) {
                area = areaService.selectByPrimaryKey(id);
                model.addAttribute("ar", area);
            }
            model.addAttribute(OPERATE_TYPE, "add");
        }
        return "single/area/edit";
    }

    /**
     * update
     *
     * @return
     */
    @RequestMapping(value = UPDATE_FILED + "/update")
    public
    @ResponseBody
    Message update(@Valid Area area) {
        Assert.notNull(area.getId(), "参数错误");
        Area updateArea = areaService.selectByPrimaryKey(area.getId());
        if (updateArea != null) {
            updateArea.setName(area.getName());
            updateArea.setCode(area.getCode());
            updateArea.setDescription(area.getDescription());
            updateArea.setModifyTime(new Date());
            updateArea.setModifyBy(area.getModifyBy());
            updateArea.setIsEnabled(area.getIsEnabled());
            updateArea.setSort(area.getSort());
            areaService.updateByPrimaryKeySelective(updateArea);
        }

        Message message = new Message(StatusType.SUCCESS.getKey(), "更新成功");
        return message;
    }

    @RequestMapping(value = DELETE_FILED + "/delete")
    public
    @ResponseBody
    Message delete(@RequestParam(value = "ids[]") String[] ids) {
        areaService.delete(ids);
        Message message = new Message(StatusType.SUCCESS.getKey(), "删除成功");
        return message;
    }

    @RequestMapping(value = VIEW_FILED + "/toTreeList")
    public String toTreeList() {
        return "single/area/areaTreeList";
    }

    @RequestMapping(VIEW_FILED + "/treeList")
    @ResponseBody
    public String treeList(String callback, String name) {
        Condition condition = new Condition();
        if (StringUtils.isNotBlank(name)) {
            condition.put("areaName", name);
        }
        List<ZTreeNoneAjaxVo> list = areaService.selectTreeArea(condition);

        return JsonMapper.nonEmptyMapper().toJsonP(callback, list);
    }

}